<?php
/**
 * Created by PhpStorm.
 * User: ciwei
 * Date: 2017/12/4
 * Time: 15:06
 */
namespace app\index\model;

use \think\Model;

class Collect extends Model
{
    public function checkCollect($data)
    {
        $duid = $data['duid'];
        $data['uid'] = session('uid');
        unset($data['duid']);
        $delRes = db('collect')
            ->where($data)
            ->delete();
        if ($delRes){
            db('msg')
                ->where('suid',$data['uid'])
                ->where('duid',$duid)
                ->where('qid',$data['qid'])
                ->delete();
            exit(json_encode(['error'=>0,'info'=>'取消收藏']));
        }
        $data['ctime'] = time();
        $res = db('collect')->insert($data);
        if($res){
            $data['status'] = 2;
            $data['duid'] = $duid;
            $data['suid'] = $data['uid'];
            unset($data['uid']);
            db('msg')->insert($data);
            exit(json_encode(['error'=>0,'info'=>'收藏成功']));
        }else{
            exit(json_encode(['error'=>1,'info'=>'收藏失败']));
        }

    }
    public function getTag($qid)
    {
        $tag = Collect::get([
            'qid'=>$qid,
            'uid'=>session('uid'),
        ]);
        return $tag;
    }
}